# -*- coding: utf-8 -*-
import scrapy
import json


class JianyuSpider(scrapy.Spider):
    name = 'jianyu'
    allowed_domains = ['www.jianyu360.com']

    # 存储的格式
    sql_data = dict(
        projectcode='',  # 项目编号
        web='',  # 信息来源网站
        keyword='',  # 关键字
        detail_url='',  # 招标详细页网址
        title='',  # 第三方网站发布标题
        toptype='',  # 信息类型
        province='',  # 归属省份
        product='',  # 产品范畴
        industry='',  # 归属行业
        tendering_manner='',  # 招标方式
        publicity_date='',  # 招标公示日期
        expiry_date='',  # 招标截止时间
    )


    # from 表单
    form_data = dict(
        pageNumber='1',
        reqType='bidSearch',
        searchvalue="路由器",
        area='',
        subtype='',
        publishtime='',
        selectType='title',
        minprice='',
        maxprice='',
        industry='',
        tabularflag='Y',
    )

    # 表单2
    form_data2 = dict(
        kds='路由器',
        industry='信息技术',
    )

    def start_requests(self):
        from_data = self.form_data2
        yield scrapy.FormRequest(
            url='https://www.jianyu360.com/article/content/ABCY2FFcSkoLD8sAnBhcHUJJzACHj1mZnB%2FPD8jKiENf3tzdR1UCds%3D.html',
            formdata=from_data,
            callback=self.parse_page2,
            dont_filter=True,
        )



        # form_data = self.form_data
        # yield scrapy.FormRequest(
        #     url='https://www.jianyu360.com/front/pcAjaxReq',
        #     formdata=form_data,
        #     callback=self.parse_page1,
        #     dont_filter=True,
        # )

    def parse_page1(self, response):
        sites = json.loads(response.body_as_unicode())
        jianyu_list = sites['list']
        # print(response.body.decode('utf-8'))
        # print(sites['list'])
        for item in jianyu_list:
            detail_url = item['_id']
            title = item['title']
            province = item['area']
            industry = item['industry']
            toptype = item['subtype']
            print(detail_url, title, province, industry, toptype)

    def parse_page2(self, response):
        print(response.body.decode('utf-8'))

